﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using MyWebApp.Models;

namespace MyWebApp.Controllers
{
    public class ContactController : Controller
    {
        private static List<Contact> _contactList;

        static ContactController()
        {

            _contactList = new List<Contact>()
            {
                new Contact(){ Id=Guid.NewGuid().ToString(), Name="张三", PhoneNo="123", EmailAddress="zhangsan@gmail.com" },
                new Contact(){ Id=Guid.NewGuid().ToString(), Name="李四", PhoneNo="456", EmailAddress="lisi@gmail.com" },
            };
        }

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public JsonResult Get()
        {
            var rst = _contactList;
            var debug = Json(rst, JsonRequestBehavior.AllowGet);
            return Json(rst, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult Get(string idx)
        {
            var rst = _contactList.FirstOrDefault(t => t.Id == idx);
            return Json(rst);
        }
        [HttpPost]
        public JsonResult Add(Contact contact)
        {
            _contactList.Add(contact);
            return Get();
        }

        [HttpPost]
        public JsonResult Update(Contact contact)
        {
            Delete(contact.Id);
            Add(contact);
            return Get();
        }

        [HttpPost]
        public JsonResult Delete(string id)
        {
            var todel = _contactList.FirstOrDefault(t => t.Id == id);
            _contactList.Remove(todel);
            return Get();
        }


    }
}
